#pragma once
#include"HashTable.h"

namespace xlf
{
	template<class K, class Hash = HashFunc<K>>
	class Unordered_Set
	{
		struct SetKeyOfT
		{
			const K& operator()(const K& key)// 仿函数 取 pair 的 first
			{
				return key;
			}
		};

	public:
		//const防止key被修改
		typedef typename hash_bucket::HashTable<K, const K, SetKeyOfT, Hash>::Iterator iterator;
		typedef typename hash_bucket::HashTable<K, const K, SetKeyOfT, Hash>::ConstIterator const_iterator;

		iterator begin()
		{
			return _ht.Begin();
		}

		iterator end()
		{
			return _ht.End();
		}

		const_iterator begin() const
		{
			return _ht.Begin();
		}

		const_iterator end() const
		{
			return _ht.End();
		}

		pair<iterator, bool> insert(const K& key)
		{
			return _ht.Insert(key);
		}

		iterator find(const K& key)
		{
			return _ht.Find(key);
		}

		iterator erase(const K& key)
		{
			return _ht.Erase(key);
		}

	private:
		hash_bucket::HashTable<K, const K, SetKeyOfT, Hash> _ht;
	};

	void Print(const Unordered_Set<int>& s)
	{
		Unordered_Set<int>::const_iterator it = s.begin();
		while (it != s.end())
		{
			//*it += 1;//const迭代器不可以修改
			cout << *it << " ";
			++it;
		}
		cout << endl;
	}

	struct Date
	{
		int _year;
		int _month;
		int _day;

		bool operator==(const Date& d) const
		{
			return _year == d._year
				&& _month == d._month
				&& _day == d._day;
		}
	};

	struct HashDate
	{
		size_t operator()(const Date& key)
		{
			//减少重复（冲突）
			//1月12日
			//12月1日
			return ((key._year * 31 + key._month) * 31 + key._day);
		}
	};

	void test_set()
	{
		Unordered_Set<int> s;
		int a[] = { 4,6,1,3,5,15,7,16,14,4,4,5 };
		for (auto e : a)
		{
			s.insert(e);
		}

		for (auto e : s)
		{
			cout << e << " ";
		}
		cout << endl;

		Unordered_Set<int>::iterator it = s.begin();
		while (it != s.end())
		{
			//*it += 1;//建议不修改key,会导致后面遍历的时候出现问题
			cout << *it << " ";
			++it;
		}
		cout << endl;

		Unordered_Set<Date, HashDate> us;
		us.insert({ 2024,10,11 });
		us.insert({ 2024,10,12 });

		Print(s);
	}

	
}